package com.tencreat.mapper.account;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.domain.account.MallUser;
import com.tencreat.domain.account.vo.AccountMyTeamVo;
import com.tencreat.domain.account.vo.AppMyTeamInfoVO;
import com.tencreat.domain.account.vo.MallUsellVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户Mapper接口
 *
 * @author tencreat
 * @date 2021-08-13
 */
public interface MallUserMapper extends BaseMapper<MallUser> {
    List<MallUser> selectNewUserList(MallUser mallUser);

    /**
     * 根据编号查询是否有下级用户
     */
    @Select("select*from mall_user where referrer_uuid=#{mallUserVO.uuid}")
    List<MallUser> selectOneByUuid(Long id);

    /**
     * 根据编号查询是否有下级用户
     */
    @Select("select*from mall_user where uuid=#{mallUserVO.uuid}")
    MallUser selectmobileByUuid(Long id);

    /**
     * 组织架构第一次查询没有上级用户id的用户
     */
    @Select("select*from mall_user where referrer_uuid =0")
    List<MallUser> selectALLOneByUuid();

    /**
     * 查询非合伙人用户
     * @return 非合伙人用户
     */
    @Select("select*from mall_user u left join mall_partner m on m.uuid = u.uuid where m.uuid is null ")
    List<MallUser> selectpartner();

    @Select("select * from mall_user mu where mu.class_relations like concat('%',#{mallUserVO.uuid},'%')")
    List<MallUser> selectOneByUuidDown(Long id);

    /**
     * 查询非合伙人用户
     * @return 非合伙人用户
     */
    @Select("select*from mall_user u left join mall_supplier m on m.uuid = u.uuid where m.uuid is null ")
    List<MallUser> selectSupplier();

    /**
     * 查询所有用户
     */
    @Select("select*from mall_user")
    List<MallUser> queryAll();

    /**
     * 锁用户
     * @param uuid 用户uuid
     * @return 用户对象
     */
    @Select("select * from mall_user where uuid = #{uuid} and del_flag = 0 for update")
    MallUser selectForUpdate(Long uuid);

    /**
     * 增加结算中余额
     * 减少传负值即可
     * @param uuid 用户ID
     * @param money 金额
     */
    @Update("update mall_user set waiting_balance = waiting_balance + #{money} where uuid = #{uuid}")
    void addWaitingBalance(@Param("uuid") Long uuid, @Param("money") BigDecimal money);

    /**
     * 增加余额
     * @param uuid 用户ID
     * @param money 金额
     */
    @Update("update mall_user set balance = balance + #{money} where uuid = #{uuid}")
    void addBalance(@Param("uuid") Long uuid, @Param("money") BigDecimal money);

    /**
     * 扣除结算中余额，增加余额
     * @param uuid 用户ID
     * @param money 金额
     */
    @Update("update mall_user set balance = balance + #{money}, waiting_balance = waiting_balance - #{money} where uuid = #{uuid}")
    void addBalanceSubtractWaitingBalance(@Param("uuid") Long uuid, @Param("money") BigDecimal money);

    @InterceptorIgnore(tenantLine = "1")
    @Select("SELECT\n" +
            "\tDATA.* \n" +
            "FROM\n" +
            "\t(\n" +
            "\tSELECT\n" +
            "\t\t@ids AS _ids,\n" +
            "\t\t( SELECT @ids := GROUP_CONCAT( uuid ) FROM mall_user WHERE FIND_IN_SET( referrer_uuid, @ids ) ) AS cids,\n" +
            "\t\t@l := @l + 1 AS LEVEL \n" +
            "\tFROM\n" +
            "\t\tmall_user,\n" +
            "\t\t( SELECT @ids := #{uuid}, @l := 0 ) b \n" +
            "\tWHERE\n" +
            "\t\t@ids IS NOT NULL \n" +
            "\t) ID,\n" +
            "\tmall_user DATA \n" +
            "WHERE\n" +
            "\tFIND_IN_SET( DATA.uuid, ID._ids ) \n" +
            "\tand data.del_flag='0'\n" +
            "ORDER BY\n" +
            "\tuuid")
    List<MallUser> queryMyTeam(@Param("uuid") Long uuid);

    void addBuyIntegral(@Param("uuid") Long uuid, @Param("integral") Long integral);

    void addCashIntegral(@Param("uuid") Long uuid, @Param("integral") Long integral);

    void reduceBuyIntegral(@Param("uuid") Long uuid, @Param("integral") Long integral);

    void reduceCashIntegral(@Param("uuid") Long uuid, @Param("integral") Long integral);

    Long selectApprovalStatus(Long uuid);

    Long selectStoreApprovalStatus(Long uuid);

    BigDecimal waitGoodsRoll();

    BigDecimal goodsRoll();

    Page<AccountMyTeamVo> appSelectOneTeam(Page<AccountMyTeamVo> page1, @Param("uuid") Long uuid);

    Page<AccountMyTeamVo> appSelectTwoTeam(Page<AccountMyTeamVo> page2, @Param("uuid") Long uuid);

    Page<AppMyTeamInfoVO> appSelectTeam(Page<AppMyTeamInfoVO> page, @Param("uuid") Long uuid);
}
